var PATH_PREFIX = "";

function setPathPrefix(path) {
	PATH_PREFIX = path;
}

var OUT_USER_ADD = function(){
	this.init();
};
/**
 * 初始化方法
 */
OUT_USER_ADD.prototype.init = function(){
	var self = this;
	self.expand();
	
	WASP_WIDGET.triggerDateStyleWithYMD("#dueDate");
	
	$("#empType").select2({placeholder: "请选择人员类型"});
	$("#empType").on("change", function(){
		$(this).valid && $(this).valid();
	});

	WASP_WIDGET.triggerOrgChilds("secondOrgname", $("#orgId").val());
	
	$("#orgId").select2({placeholder: "请选择所属机构"});
	$("#orgId").on("change", function(){
		$(this).valid && $(this).valid();
		WASP_WIDGET.triggerOrgChilds("secondOrgname", $("#orgId").val());
	});
	
	$("#secondOrgname").on("select2:select", function(e) {
		var id = e.params.data.id;
		var text = e.params.data.text;
		if(id.indexOf("NewTag") != -1) {  // 新增的
			$("#secondOrgid").removeAttr("readonly");
			$("#secondOrgid").val('');
		} else {
			$("#secondOrgid").attr("readonly", true);
			$("#secondOrgid").val(id.split("_")[0]);
		}
		$("#secondOrgid").valid && $("#secondOrgid").valid();
		
		if(!$("#secondOrgid").val()) {  // 没有部门
			WASP_WIDGET.triggerTitleChilds("titleName", "QAQ_O.O");  // 随便搞个，让它查不出
		} else {  // 已有部门
			WASP_WIDGET.triggerTitleChilds("titleName", $("#secondOrgid").val());
		}
	}); 
	
	if(!$("#secondOrgid").val()) {  // 没有部门
		WASP_WIDGET.triggerTitleChilds("titleName", "QAQ_O.O");  // 随便搞个，让它查不出
	} else {  // 已有部门
		WASP_WIDGET.triggerTitleChilds("titleName", $("#secondOrgid").val());
	}
	
	$("#secondOrgname").on("change", function(){
		$(this).valid && $(this).valid();
	});
	
	$("#titleName").on("select2:select", function(e) {
		var id = e.params.data.id;
		var text = e.params.data.text;
		if(id.indexOf("NewTag") != -1) {  // 新增的
			$("#titleId").removeAttr("readonly");
			$("#titleId").val('');
		} else {
			$("#titleId").attr("readonly", true);
			$("#titleId").val(id.split("_")[0]);
		}
		
		$("#titleId").valid && $("#titleId").valid();
	}); 
	
	$("#titleName").on("change", function() {
		$(this).valid && $(this).valid();
	});

	$("#outUserAddForm").validate({
		focusCleanup : true
	});
	
	$("#empName").blur(function() {
		$.post(PATH_PREFIX + "generateLogin.do", {emName: $("#empName").val()}, function(data) {
			$("#login").val(data.object);
			$("#login").valid && $("#login").valid();
		}, 'json');
	});
//	$("#secondOrgname").blur(function() {
//		$.post("/solution/sys/organization/generateOrgIdPinyin.do", {orgName: $("#secondOrgname").val()}, function(data) {
//			$("#secondOrgid").val(data.object);
//			$("#secondOrgid").valid && $("#secondOrgid").valid();
//		}, 'json');
//	});
//	$("#titleName").blur(function() {
//		$.post("/solution/sys/organization/generateOrgIdPinyin.do", {orgName: $("#titleName").val()}, function(data) {
//			$("#titleId").val(data.object);
//			$("#titleId").valid && $("#titleId").valid();
//		}, 'json');
//	});
	
	self.validate();
}

/**
 * 拓展方法
 */
OUT_USER_ADD.prototype.expand = function(){
	var self = this;
	
	self.validate = function() {
		$("#empName").rules("remove");
		$("#login").rules("remove");
		$("#password").rules("remove");
		$("#empType").rules("remove");
		$("#orgId").rules("remove");
		$("#secondOrgname").rules("remove");
		$("#secondOrgid").rules("remove");
		$("#titleName").rules("remove");
		$("#titleId").rules("remove");
		
		$("#empName").rules("add", {
			required : true, maxlength4Byte: 100 , messages: {required: "请填写姓名", maxlength4Byte:"姓名不能大于100个字符"}
		});
		$("#login").rules("add", {
			required : true, maxlength4Byte: 20, login:true , messages: {required: "请填写账号", maxlength4Byte: "账号不能大于20个字符"}
		});
		$("#password").rules("add", {
			required : true, sPassword: true , messages: {required: "请填写密码"}
		});
		$("#empType").rules("add", {
			required : true, messages: {required: "请选择人员类型"}
		});
		$("#orgId").rules("add", {
			required : true, messages: {required: "请选择所属机构"}
		});
		$("#secondOrgname").rules("add", {
			required : true, maxlength4ByteMutilS2: 500 , messages: {required: "请选择所属部门", maxlength4ByteMutilS2:"部门名称不能大于500个字符"}
		});
		$("#secondOrgid").rules("add", {
			required : true, orgId: true , messages: {required: "请填写部门代码"}
		});
		$("#titleName").rules("add", {
			required : true, maxlength4ByteMutilS2: 500 , messages: {required: "请选择所属岗位", maxlength4ByteMutilS2:"岗位名称不能大于500个字符"}
		});
		$("#titleId").rules("add", {
			required : true, titleId: true , messages: {required: "请填写岗位代码"}
		});
	};
	
	self.submitForm = function() {
		SubmitAndPreventSecond("outUserAddForm",function() {
			self.validate();
			
			if(!$("form[name='outUserAddForm']").valid()) {
				toastr.warning('', '请完善信息');
				return false;
			}
			
			if($("#empType").val() == "IN") {
				toastr.warning('', '不允许新增内部人员！');
				return false;
			}
			
			var orgNames = $("#orgId option:selected").text().split("/");
			$("#orgName").val(orgNames[orgNames.length - 1]);
			
			if($("#secondOrgid").attr("readonly")) {
				$("#existSecondOrg").val(true);
			}
			
			$('#outUserAddForm').attr('action', PATH_PREFIX + "insertOutUser.do");
			
			return true;
		}, true);
	};
}

jQuery.validator.addMethod("orgId", function(value, element, param) { 
	if($("#secondOrgid").attr("readonly")) {  // 选择已有部门的时候不用校验
		return true;
	}
	
	var orgReg = /^[a-zA-Z0-9]{1,10}$/;
    var flag = this.optional(element) || (orgReg.test(value));
    if(!flag) {
    	jQuery.validator.messages.orgId = "部门代码只能输入数字或者字母，长度不能大于10个字符";
    	return false;
    }
    
    $.ajax({
		  url: "/solution/sys/organization/checkRepeatOrgId.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  orgId : value
		  },
		  success: function( data ) {
				if(data.success){
					flag = false;
					return;
				}
				flag = true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				flag = false;
		  }
	});
    if(!flag) {
    	jQuery.validator.messages.orgId = "部门代码已存在";
    }
    return flag;
});
jQuery.validator.addMethod("titleId", function(value, element, param) { 
	if($("#titleId").attr("titleId")) {  // 选择已有岗位的时候不用校验
		return true;
	}
	
	var orgReg = /^[a-zA-Z0-9]{1,10}$/;
    var flag = this.optional(element) || (orgReg.test(value));
    if(!flag) {
    	jQuery.validator.messages.titleId = "岗位代码只能输入数字或者字母，长度不能大于10个字符";
    	return false;
    }
    
    $.ajax({
		  url: "/solution/sys/organization/checkRepeatTitleId.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  titleId : value,
			  orgId: $("#secondOrgid").val()
		  },
		  success: function( data ) {
				if(data.success){
					flag = false;
					return;
				}
				flag = true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				flag = false;
		  }
	});
	if(!flag) {
		jQuery.validator.messages.titleId = "岗位代码已存在";
	}
    return true;
});
jQuery.validator.addMethod("maxlength4ByteMutilS2", function(value, element, param) { 
	   var flag = true;
	   for(var i = 0; i < value.length; i++) {
		   var length = value[i].replace(/ /ig,'a').replace(/[^x00-xff]/ig,'aa').length;
		   if (length > param) {
			   flag = false;
			   break;
		   }
	   }
		return flag;
	}, "您输入的文本不能大于{0}个字符（一个汉字代表两个字符）");
jQuery.validator.addMethod("login", function(value, element, param) { 
    $.ajax({
		  url: PATH_PREFIX + "checkRepeatLogin.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  login : value
		  },
		  success: function( data ) {
				if(data.success){
					flag = false;
					return;
				}
				flag = true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				flag = false;
		  }
	});
    if(!flag) {
    	jQuery.validator.messages.login = "账号已存在";
    }
    return flag;
});
